home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / ToolManager / Source / Prefs / global.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  13KB  |  421 lines

  1. /*
  2.  * global.c  V3.1
  3.  *
  4.  * ToolManager global settings
  5.  *
  6.  * Copyright (C) 1990-98 Stefan Becker
  7.  *
  8.  * This source code is for educational purposes only. You may study it
  9.  * and copy ideas or algorithms from it for your own projects. It is
  10.  * not allowed to use any of the source codes (in full or in parts)
  11.  * in other programs. Especially it is not allowed to create variants
  12.  * of ToolManager or ToolManager-like programs from this source code.
  13.  *
  14.  */
  15.  
  16. #include "toolmanager.h"
  17.  
  18. /* Local data */
  19. #define PROPCHUNKS 3
  20. static const ULONG PropChunksTable[2 * PROPCHUNKS] = {
  21.  ID_TMGP, ID_DATA,
  22.  ID_TMGP, ID_CDIR,
  23.  ID_TMGP, ID_CMND
  24. };
  25. static const char *TextTitle;
  26. static const char *HelpDirectory;
  27. static const char *TextPreferences;
  28. static const char *HelpPreferences;
  29. static const char *TextNetwork;
  30. static const char *HelpNetwork;
  31. static const char *TextRemap;
  32. static const char *HelpRemap;
  33. static const char *TextRemapPrecision;
  34. static const char *HelpRemapPrecision;
  35. static const char *TextPrecisionTypes[DATA_GLOBAL_PRECISION_MAX + 1];
  36. static Object     *Window          = NULL;
  37. static char       *GlobalDirectory = NULL;
  38. static char       *GlobalCommand   = NULL;
  39. static ULONG       GlobalFlags     = DATA_GLOBALF_REMAPENABLE;
  40. static ULONG       GlobalPrecision = DATA_GLOBAL_PRECISION_DEFAULT;
  41.  
  42. /* Global class instance data */
  43. struct GlobalClassData {
  44.  Object *gcd_Directory;
  45.  Object *gcd_Command;
  46.  Object *gcd_Network;
  47.  Object *gcd_Remap;
  48.  Object *gcd_Precision;
  49. };
  50. #define TYPED_INST_DATA(cl, o) ((struct GlobalClassData *) INST_DATA((cl), (o)))
  51.  
  52. /* Read global configuration data */
  53. #undef  DEBUGFUNCTION
  54. #define DEBUGFUNCTION ParseGlobalIFF
  55. BOOL ParseGlobalIFF(struct IFFHandle *iffh)
  56. {
  57.  BOOL rc = FALSE;
  58.  
  59.  if ((PropChunks(iffh, PropChunksTable, PROPCHUNKS) == 0) &&
  60.      (StopOnExit(iffh, ID_TMGP, ID_FORM) == 0)  &&
  61.      (ParseIFF(iffh, IFFPARSE_SCAN) == IFFERR_EOC)) {
  62.   struct StoredProperty *sp;
  63.  
  64.   GLOBAL_LOG(LOG0(FORM TMGP chunk parsed OK))
  65.  
  66.   /* Get DATA chunk */
  67.   if (sp = FindProp(iffh, ID_TMGP, ID_DATA)) {
  68.    struct GlobalDATAChunk *gdc = (struct GlobalDATAChunk *) sp->sp_Data;
  69.  
  70.    GLOBAL_LOG(LOG2(Data, "Flags 0x%08lx Precision %ld", gdc->gdc_Flags,
  71.                    gdc->gdc_Precision))
  72.  
  73.    /* Copy data */
  74.    GlobalFlags     = gdc->gdc_Flags & DATA_GLOBALF_MASK;
  75.    GlobalPrecision = gdc->gdc_Precision;
  76.  
  77.    /* Sanity Check */
  78.    if (GlobalPrecision >= DATA_GLOBAL_PRECISION_MAX)
  79.     GlobalPrecision = DATA_GLOBAL_PRECISION_DEFAULT;
  80.   }
  81.  
  82.   /* Get string values */
  83.   GlobalDirectory = ReadStringProperty(iffh, ID_TMGP, ID_CDIR);
  84.   GlobalCommand   = ReadStringProperty(iffh, ID_TMGP, ID_CMND);
  85.  
  86.   /* Chunk OK */
  87.   rc = TRUE;
  88.  }
  89.  
  90.  GLOBAL_LOG(LOG1(Result, "%ld", rc))
  91.  
  92.  return(rc);
  93. }
  94.  
  95. /* Write global configuration data */
  96. #undef  DEBUGFUNCTION
  97. #define DEBUGFUNCTION WriteGlobalIFF
  98. BOOL WriteGlobalIFF(struct IFFHandle *iffh)
  99. {
  100.  struct GlobalDATAChunk gdc;
  101.  
  102.  /* Initialize DATA chunk */
  103.  gdc.gdc_Flags     = GlobalFlags;
  104.  gdc.gdc_Precision = GlobalPrecision;
  105.  
  106.  BOOL rc = (PushChunk(iffh, ID_TMGP, ID_FORM, IFFSIZE_UNKNOWN) == 0) &&
  107.            WriteProperty(iffh, ID_DATA, &gdc,
  108.                          sizeof(struct GlobalDATAChunk))             &&
  109.            WriteStringProperty(iffh, ID_CDIR, GlobalDirectory)       &&
  110.            WriteStringProperty(iffh, ID_CMND, GlobalCommand)         &&
  111.            (PopChunk(iffh) == 0);
  112.  
  113.  GLOBAL_LOG(LOG1(Result, "%ld", rc))
  114.  
  115.  return(rc);
  116. }
  117.  
  118. /* Free global data */
  119. void FreeGlobalData(void)
  120. {
  121.  /* Free strings */
  122.  if (GlobalDirectory) FreeVector(GlobalDirectory);
  123.  if (GlobalCommand)   FreeVector(GlobalCommand);
  124. }
  125.  
  126. /* Global class method: OM_NEW */
  127. #define DEBUGFUNCTION GlobalClassNew
  128. static ULONG GlobalClassNew(Class *cl, Object *obj, struct opSet *ops)
  129. {
  130.  Object *Directory;
  131.  Object *Command;
  132.  Object *Network;
  133.  Object *Remap;
  134.  Object *Precision;
  135.  Object *Use;
  136.  Object *Cancel;
  137.  
  138.  GLOBAL_LOG((LOG1(Tags, "0x%08lx", ops->ops_AttrList),
  139.              PrintTagList(ops->ops_AttrList)))
  140.  
  141.  /* Create object */
  142.  if (obj = (Object *) DoSuperNew(cl, obj,
  143.           MUIA_Window_Title, TextTitle,
  144.           MUIA_Window_ID,    MAKE_ID('G','L','O','B'),
  145.           WindowContents,    VGroup,
  146.            Child, ColGroup(2),
  147.             Child, Label2(TextGlobalDirectory),
  148.             Child, Directory = TMPopFile(TextGlobalSelectDir, GlobalDirectory,
  149.                                          LENGTH_FILENAME, HelpDirectory),
  150.              ASLFR_DrawersOnly, TRUE,
  151.             End,
  152.             Child, Label2(TextPreferences),
  153.             Child, Command   = TMPopFile(TextGlobalSelectCmd, GlobalCommand,
  154.                                          LENGTH_FILENAME, HelpPreferences),
  155.             End,
  156.            End,
  157.            Child, ColGroup(2),
  158.             Child, Label1(TextNetwork),
  159.             Child, Network =
  160.              MakeCheckmark(GlobalFlags & DATA_GLOBALF_NETWORKENABLE,
  161.                            HelpNetwork),
  162.             Child, Label1(TextRemap),
  163.             Child, Remap =
  164.              MakeCheckmark(GlobalFlags & DATA_GLOBALF_REMAPENABLE,
  165.                            HelpRemap),
  166.             Child, Label2(TextRemapPrecision),
  167.             Child, Precision = CycleObject,
  168.              MUIA_Cycle_Entries, TextPrecisionTypes,
  169.              MUIA_Cycle_Active,  GlobalPrecision,
  170.              MUIA_CycleChain,    TRUE,
  171.              MUIA_ShortHelp,     HelpRemapPrecision,
  172.             End,
  173.            End,
  174.            Child, HGroup,
  175.             Child, Use    = MakeButton(TextGlobalUse,    HelpGlobalUse),
  176.             Child, HSpace(0),
  177.             Child, Cancel = MakeButton(TextGlobalCancel, HelpGlobalCancel),
  178.            End,
  179.           End,
  180.           MUIA_HelpNode,     "GlobalWindow",
  181.           TAG_MORE,          ops->ops_AttrList)) {
  182.   struct GlobalClassData *gcd = TYPED_INST_DATA(cl, obj);
  183.  
  184.   /* Initialize instance data */
  185.   gcd->gcd_Directory = Directory;
  186.   gcd->gcd_Command   = Command;
  187.   gcd->gcd_Network   = Network;
  188.   gcd->gcd_Remap     = Remap;
  189.   gcd->gcd_Precision = Precision;
  190.  
  191.   /* Close window action */
  192.   DoMethod(obj,    MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
  193.            MUIV_Notify_Application, 4, MUIM_Application_PushMethod,
  194.            obj, 2, TMM_Finish, TMV_Finish_Cancel);
  195.  
  196.   /* Gadget actions */
  197.   DoMethod(gcd->gcd_Remap, MUIM_Notify, MUIA_Selected, MUIV_EveryTime,
  198.            obj, 1, TMM_Change);
  199.  
  200.   /* Button actions */
  201.   DoMethod(Use,    MUIM_Notify, MUIA_Pressed, FALSE,
  202.            MUIV_Notify_Application, 5, MUIM_Application_PushMethod,
  203.            obj, 2, TMM_Finish, TMV_Finish_Use);
  204.   DoMethod(Cancel, MUIM_Notify, MUIA_Pressed, FALSE,
  205.            MUIV_Notify_Application, 5, MUIM_Application_PushMethod,
  206.            obj, 2, TMM_Finish, TMV_Finish_Cancel);
  207.  
  208.   /* Set initial disable states */
  209.   DoMethod(obj, TMM_Change);
  210.  }
  211.  
  212.  GLOBAL_LOG(LOG1(Result, "0x%08lx", obj))
  213.  
  214.  /* Return pointer to created object */
  215.  return((ULONG) obj);
  216. }
  217.  
  218. /* Global class method: TMM_Finish */
  219. #undef  DEBUGFUNCTION
  220. #define DEBUGFUNCTION GlobalClassFinish
  221. static ULONG GlobalClassFinish(Class *cl, Object *obj,
  222.                                    struct TMP_Finish *tmpf)
  223. {
  224.  /* Close window */
  225.  SetAttrs(obj, MUIA_Window_Open, FALSE, TAG_DONE);
  226.  
  227.  /* Use new global settings? */
  228.  if (tmpf->tmpf_Type == TMV_Finish_Use) {
  229.   struct GlobalClassData *gcd = TYPED_INST_DATA(cl, obj);
  230.  
  231.   /* Get new string contents */
  232.   GlobalDirectory = GetStringContents(gcd->gcd_Directory, GlobalDirectory);
  233.   GlobalCommand   = GetStringContents(gcd->gcd_Command, GlobalCommand);
  234.  
  235.   /* Get new flag states */
  236.   GlobalFlags = GetCheckmarkState(gcd->gcd_Network,
  237.                                   DATA_GLOBALF_NETWORKENABLE) |
  238.                 GetCheckmarkState(gcd->gcd_Remap,
  239.                                   DATA_GLOBALF_REMAPENABLE);
  240.  
  241.   /* Get new precision type */
  242.   GetAttr(MUIA_Cycle_Active, gcd->gcd_Precision, &GlobalPrecision);
  243.  }
  244.  
  245.  /* Remove window from application */
  246.  DoMethod(_app(obj), OM_REMMEMBER, obj);
  247.  
  248.  /* Dispose object */
  249.  MUI_DisposeObject(obj);
  250.  
  251.  /* Clear global window pointer */
  252.  Window = NULL;
  253.  
  254.  /* Return 1 to indicate that the method is implemented */
  255.  return(1);
  256. }
  257.  
  258. /* Global class method: TMM_Change */
  259. #undef  DEBUGFUNCTION
  260. #define DEBUGFUNCTION GlobalClassChange
  261. static ULONG GlobalClassChange(Class *cl, Object *obj)
  262. {
  263.  struct GlobalClassData *gcd = TYPED_INST_DATA(cl, obj);
  264.  
  265.  GLOBAL_LOG(LOG0(Entry))
  266.  
  267.  /* Precision cycle gadget is disabled if remap is disabled */
  268.  SetDisabledState(gcd->gcd_Precision,
  269.                   !GetCheckmarkState(gcd->gcd_Remap, TRUE));
  270.  
  271.  /* Return 1 to indicate that the method is implemented */
  272.  return(1);
  273. }
  274.  
  275. /* Global class method dispatcher */
  276. #undef  DEBUGFUNCTION
  277. #define DEBUGFUNCTION GlobalClassDispatcher
  278. __geta4 static ULONG GlobalClassDispatcher(__a0 Class *cl, __a2 Object *obj,
  279.                                            __a1 Msg msg)
  280. {
  281.  ULONG rc;
  282.  
  283.  GLOBAL_LOG(LOG3(Arguments, "Class 0x%08lx Object 0x%08lx Msg 0x%08lx",
  284.                  cl, obj, msg))
  285.  
  286.  switch(msg->MethodID) {
  287.   /* BOOPSI methods */
  288.   case OM_NEW:
  289.    rc = GlobalClassNew(cl, obj, (struct opSet *) msg);
  290.    break;
  291.  
  292.   /* TM methods */
  293.   case TMM_Finish:
  294.    rc = GlobalClassFinish(cl, obj, (struct TMP_Finish *) msg);
  295.    break;
  296.  
  297.   case TMM_Change:
  298.    rc = GlobalClassChange(cl, obj);
  299.    break;
  300.  
  301.   /* Unknown method -> delegate to SuperClass */
  302.   default:
  303.    rc = DoSuperMethodA(cl, obj, msg);
  304.    break;
  305.  }
  306.  
  307.  return(rc);
  308. }
  309.  
  310. /* Create Global class */
  311. #undef  DEBUGFUNCTION
  312. #define DEBUGFUNCTION CreateGlobalClass
  313. struct MUI_CustomClass *CreateGlobalClass(void)
  314. {
  315.  struct MUI_CustomClass *rc;
  316.  
  317.  /* Create class */
  318.  if (rc = MUI_CreateCustomClass(NULL, MUIC_Window, NULL,
  319.                                 sizeof(struct GlobalClassData),
  320.                                 GlobalClassDispatcher)) {
  321.  
  322.   /* Localize strings */
  323.   TextTitle             = TranslateString(
  324.                                        LOCALE_TEXT_GLOBAL_WINDOW_TITLE_STR,
  325.                                        LOCALE_TEXT_GLOBAL_WINDOW_TITLE);
  326.   HelpDirectory         = TranslateString(
  327.                                        LOCALE_HELP_GLOBAL_DIRECTORY_STR,
  328.                                        LOCALE_HELP_GLOBAL_DIRECTORY);
  329.   TextPreferences       = TranslateString(
  330.                                        LOCALE_TEXT_GLOBAL_PREFERENCES_STR,
  331.                                        LOCALE_TEXT_GLOBAL_PREFERENCES);
  332.   HelpPreferences       = TranslateString(
  333.                                        LOCALE_HELP_GLOBAL_PREFERENCES_STR,
  334.                                        LOCALE_HELP_GLOBAL_PREFERENCES);
  335.   TextNetwork           = TranslateString(
  336.                                        LOCALE_TEXT_GLOBAL_NETWORK_STR,
  337.                                        LOCALE_TEXT_GLOBAL_NETWORK);
  338.   HelpNetwork           = TranslateString(
  339.                                        LOCALE_HELP_GLOBAL_NETWORK_STR,
  340.                                        LOCALE_HELP_GLOBAL_NETWORK);
  341.   TextRemap             = TranslateString(
  342.                                        LOCALE_TEXT_GLOBAL_REMAP_STR,
  343.                                        LOCALE_TEXT_GLOBAL_REMAP);
  344.   HelpRemap             = TranslateString(
  345.                                        LOCALE_HELP_GLOBAL_REMAP_STR,
  346.                                        LOCALE_HELP_GLOBAL_REMAP);
  347.   TextRemapPrecision    = TranslateString(
  348.                                        LOCALE_TEXT_GLOBAL_REMAP_PRECISION_STR,
  349.                                        LOCALE_TEXT_GLOBAL_REMAP_PRECISION);
  350.   HelpRemapPrecision    = TranslateString(
  351.                                        LOCALE_HELP_GLOBAL_REMAP_PRECISION_STR,
  352.                                        LOCALE_HELP_GLOBAL_REMAP_PRECISION);
  353.   TextPrecisionTypes[0] = TranslateString(
  354.                                        LOCALE_TEXT_GLOBAL_PRECISION_EXACT_STR,
  355.                                        LOCALE_TEXT_GLOBAL_PRECISION_EXACT);
  356.   TextPrecisionTypes[1] = TranslateString(
  357.                                        LOCALE_TEXT_GLOBAL_PRECISION_IMAGES_STR,
  358.                                        LOCALE_TEXT_GLOBAL_PRECISION_IMAGES);
  359.   TextPrecisionTypes[2] = TranslateString(
  360.                                        LOCALE_TEXT_GLOBAL_PRECISION_ICONS_STR,
  361.                                        LOCALE_TEXT_GLOBAL_PRECISION_ICONS);
  362.   TextPrecisionTypes[3] = TranslateString(
  363.                                        LOCALE_TEXT_GLOBAL_PRECISION_GUI_STR,
  364.                                        LOCALE_TEXT_GLOBAL_PRECISION_GUI);
  365.   TextPrecisionTypes[4] = NULL;
  366.  
  367.  }
  368.  
  369.  GLOBAL_LOG(LOG1(Result, "0x%08lx", rc))
  370.  
  371.  return(rc);
  372. }
  373.  
  374. /* Open global window */
  375. #undef  DEBUGFUNCTION
  376. #define DEBUGFUNCTION OpenGlobalWindow
  377. void OpenGlobalWindow(Object *app)
  378. {
  379.  /* Window already open? */
  380.  if (Window) {
  381.  
  382.   GLOBAL_LOG(LOG0(Move window to front))
  383.  
  384.   /* Yes, send method to window */
  385.   DoMethod(Window, MUIM_Window_ToFront);
  386.  
  387.  } else {
  388.  
  389.   /* No, create global window */
  390.   if (Window = NewObject(GlobalClass->mcc_Class, NULL, NULL)) {
  391.    ULONG opened;
  392.  
  393.    GLOBAL_LOG(LOG1(Global window, "0x%08lx", Window))
  394.  
  395.    /* Add window to application */
  396.    DoMethod(app, OM_ADDMEMBER, Window);
  397.  
  398.    /* Open main window */
  399.    SetAttrs(Window, MUIA_Window_Open, TRUE, TAG_DONE);
  400.  
  401.    /* Get window open status */
  402.    GetAttr(MUIA_Window_Open, Window, &opened);
  403.  
  404.    /* Window open? */
  405.    if (opened == FALSE) {
  406.  
  407.     GLOBAL_LOG(LOG0(Could not open global window))
  408.  
  409.     /* No, remove window from application */
  410.     DoMethod(app, OM_REMMEMBER, Window);
  411.  
  412.     /* Dispose window */
  413.     MUI_DisposeObject(Window);
  414.  
  415.     /* Clear pointer */
  416.     Window = NULL;
  417.    }
  418.   }
  419.  }
  420. }
  421.